﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using BTN04.BUS;
using BTN04.DTO;

namespace BTN04.ADMIN
{
    public partial class WebForm1 : System.Web.UI.Page
    {
         
        protected void Page_Load(object sender, EventArgs e)
        {
            //Hien thi danh sach nha san xuat len droplist
            DataTable dt1 = new DataTable();
            NhaSanXuatBUS nsxBus = new NhaSanXuatBUS();
            dt1 = nsxBus.LayDanhSachNhaSanXuat();

            //Hien thi danh sach loai san pham len droplist
            List<LoaiSanPhamDTO> ds =new List<LoaiSanPhamDTO>();
            LoaiSanPhamBUS LspBus = new LoaiSanPhamBUS();
            ds = LspBus.DanhSachLoaiSanPham();

            //Hien thi danh sach xuat xu len droplist
            DataTable dt2 = new DataTable();
           XuatSuBUS xsBus = new XuatSuBUS();
            dt2 = xsBus.LayDanhSachXuatSu();

            //Hient thi danh sach Tinh Trang Loai San Pham len dropList
            DataTable dt3 = new DataTable();
            TinhTrangSanPhamBUS trspBus = new TinhTrangSanPhamBUS();
            dt3 = trspBus.LayDanhSachTinhTrangSanPham();

            //Hien thi danh sach khuyen mai len dropList
            DataTable dt4 = new DataTable();
            KhuyenMaiBUS kmBus = new KhuyenMaiBUS();
            dt4=kmBus.LayDanhSachKhuyenMai();

            if(!IsPostBack)
            {
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    ListItem lv = new ListItem();
                   lv.Value =dt1.Rows[i]["MaNhaSanXuat"].ToString();
                    lv.Text = dt1.Rows[i]["TenNhaSanXuat"].ToString();
                    drNhaSX.Items.Add(lv);
                }
                for( int i=0;i<ds.Count;i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = ds[i].MaLoaiSanPham.ToString();
                    lv.Text = ds[i].TenLoaiSanPham.ToString();
                    drLoaiSP.Items.Add(lv);
                }
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = dt2.Rows[i]["MaXuatSu"].ToString();
                    lv.Text = dt2.Rows[i]["TenXuatSu"].ToString();
                    drXuatXu.Items.Add(lv);
                }
                for (int i = 0; i < dt3.Rows.Count; i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = dt3.Rows[i]["MaTinhTrangSanPham"].ToString();
                    lv.Text = dt3.Rows[i]["TenTinhTrangSanPham"].ToString();
                    drTinhTrangSP.Items.Add(lv);
                }
                for (int i = 0; i < dt4.Rows.Count; i++)
                {
                    ListItem lv = new ListItem();
                    lv.Value = dt4.Rows[i]["MaKhuyenMai"].ToString();
                    lv.Text = dt4.Rows[i]["TenKhuyenMai"].ToString();
                    drKhuyenMai.Items.Add(lv);
                }


            }
        


        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[5].HasControls()==true)
                    {
                        LinkButton lbtDelete = ((LinkButton)e.Row.Cells[5].Controls[0]);
                        lbtDelete.Attributes.Add("onclick", " return lbtDelete_Click(sender,e)");

                     }
                }

        }

        public int lbtDelete_Click(object sender, GridViewRowEventArgs e)
        {
          
            int ma =int.Parse((e.Row.Cells[0].Text));
            SanPhamBUS spBus = new SanPhamBUS();
            int kq = spBus.XoaMotSanPhamDuaVaoMa(ma);
            return kq;
               
             
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SanPhamBUS spBus = new SanPhamBUS();
            int index = GridView1.SelectedIndex;
            GridViewRow row = GridView1.SelectedRow;
          
            if(index>=0)
            {
                int ma = int.Parse(row.Cells[0].Text);
                SanPhamDTO spDto = new SanPhamDTO();
                spDto=spBus.HienThiCHiTietSanPhamTheoMa(ma);
                txtMaSP.Text=spDto.MaSanPham.ToString();
                txtTenSP.Text = spDto.TenSanPham.ToString();
                drLoaiSP.SelectedItem.Text = spDto.TenLoaiSanPham.ToString();
                drTinhTrangSP.SelectedItem.Text = spDto.TenTinhTrangSanPham.ToString();
                drNhaSX.SelectedItem.Text= spDto.TenNhaSanXuat.ToString();
              
                txtGiaBan.Text = spDto.GiaBan.ToString();
                txtSoLuongTon.Text = spDto.SoLuongTon.ToString();
                txtSoLuongXem.Text = spDto.SoLuongXem.ToString();
                drXuatXu.SelectedItem.Text = spDto.TenXuatSu.ToString();
                txtHinhAnh.Text = spDto.Hinh.ToString();
                txtMoTa.Text = spDto.MoTa.ToString();
                
                
                

            }
          
            
            
        }
        //xu ly khi nhan nut sua

        protected void btnSửa_Click(object sender, EventArgs e)
        {
            int index = GridView1.SelectedIndex;
            if(index>=0)
            {
                SanPhamDTO spDto = new SanPhamDTO();
                spDto.MaSanPham = int.Parse(txtMaSP.Text);
                spDto.TenSanPham = txtTenSP.Text;
                spDto.MaLoaiSanPham = int.Parse(drLoaiSP.Items[drLoaiSP.SelectedIndex].Value.ToString());
                spDto.MaTinhTrangSanPham = int.Parse(drTinhTrangSP.Items[drTinhTrangSP.SelectedIndex].Value.ToString());
                spDto.MaNhaSanXuat = int.Parse(drNhaSX.Items[drNhaSX.SelectedIndex].Value);
                spDto.GiaBan = int.Parse(txtGiaBan.Text);
                spDto.SoLuongXem = int.Parse(txtSoLuongXem.Text);
                spDto.SoLuongTon = int.Parse(txtSoLuongTon.Text);

                spDto.MaXuatSu = int.Parse(drXuatXu.Items[drXuatXu.SelectedIndex].Value.ToString());
                spDto.Hinh = txtHinhAnh.Text;
                spDto.MoTa = txtMoTa.Text;
                spDto.MaKhuyenMai = int.Parse(drKhuyenMai.Items[drKhuyenMai.SelectedIndex].Value.ToString());



                SanPhamBUS spBus = new SanPhamBUS();
                spBus.CapNhatMotSanPham(spDto);
            }
           
            
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            SanPhamDTO spDto = new SanPhamDTO();
        
            spDto.TenSanPham = txtTenSP.Text;
            spDto.MaLoaiSanPham = int.Parse(drLoaiSP.Items[drLoaiSP.SelectedIndex].Value.ToString());
            spDto.MaTinhTrangSanPham = int.Parse(drTinhTrangSP.Items[drTinhTrangSP.SelectedIndex].Value.ToString());
            spDto.MaNhaSanXuat = int.Parse(drNhaSX.Items[drNhaSX.SelectedIndex].Value);
            spDto.GiaBan = int.Parse(txtGiaBan.Text);
            spDto.SoLuongXem = int.Parse(txtSoLuongXem.Text);
            spDto.SoLuongTon = int.Parse(txtSoLuongTon.Text);

            spDto.MaXuatSu = int.Parse(drXuatXu.Items[drXuatXu.SelectedIndex].Value.ToString());
            spDto.Hinh = txtHinhAnh.Text;
            spDto.MoTa = txtMoTa.Text;
            spDto.MaKhuyenMai = int.Parse(drKhuyenMai.Items[drKhuyenMai.SelectedIndex].Value.ToString());



            SanPhamBUS spBus = new SanPhamBUS();
            spBus.ThemMotSanPham(spDto);

        }
       

     

   

   
     
    }
}
